<?php
/*
Extension Name: Approval Role
Extension Url: http://code.google.com/p/vanilla-friends/
Description: Assign any role to a new applicant on approval.
Version: 1.0.1
Author: squirrel
Author Url: http://digitalsquirrel.com/
*/


$PostBackAction = ForceIncomingString('PostBackAction', '');

// Bail out if this is not the applicants form.
if ( ('settings.php' != $Context->SelfUrl) || !in_array($PostBackAction, array('Applicants','ProcessApplicants')) ) {
	return;
}

// Get submitted values.
$ApprovalRole = ForceIncomingInt('ApprovalRole', 0);

// Save off the existing approval role.
$Configuration['APPROVAL_ROLE.old'] = $Configuration['APPROVAL_ROLE'];

// Build the list of valid approval roles.
$RoleManager = $Context->ObjectFactory->NewContextObject($Context, 'RoleManager');
$DataSet = $RoleManager->GetRoles();
// Because the list is sorted by Priority, any role that appears before the
// role of the current user is a lower priority. Only roles of a lower
// priority are valid as a role for a newly approved user.
while ( ($Row = $Context->Database->GetRow($DataSet)) && ($Row['RoleID'] != $Context->Session->User->RoleID) ) {
	$Configuration['ApprovalRole.RoleList'][] = $Row;
	// Look for the submitted role in the allowed list.
	if ( $ApprovalRole == $Row['RoleID'] ) {
	// H4X! Temporarily override the role for newly approved users.
		$Configuration['APPROVAL_ROLE'] = $ApprovalRole;
	}
}

function ApprovalRoleAddSelect(&$ApplicantsForm) {
	$Context = &$ApplicantsForm->Context;

	// Bail out if no applicants.
	if ( 0 == $Context->Database->RowCount($ApplicantsForm->ApplicantData) ) {
		return;
	}

	// Build a select control of all valid approval roles.
	$RoleSelect = $Context->ObjectFactory->NewObject($Context, 'Select');
	$RoleSelect->Name = 'ApprovalRole';
	foreach ( $Context->Configuration['ApprovalRole.RoleList'] as $key => $Role ) {
		$RoleSelect->AddOption($Role['RoleID'], $Role['Name']);
	}
	$RoleSelect->SelectedValue = $Context->Configuration['APPROVAL_ROLE.old'];
	$RoleSelect->Attributes = ' id="ddApprovalRole"';

	// H4X! Append the select directly to the 'ApplicantsNotes' definition.
	$Context->Dictionary['ApplicantsNotes'] .= '<br />'
		. $Context->GetDefinition('ApprovedMemberRole')
		. ' ' . $RoleSelect->Get();
}
$Context->AddToDelegate('ApplicantsForm', 'PreRender', 'ApprovalRoleAddSelect');


// no se apoye contra la puerta

?>
